<?php
/**
 * @link https://ym2.cleverstone.top
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace common\components\sms\sender;

use common\helper\App;
use yii\base\BaseObject;
use yii\helpers\Json;
use common\helper\Http;
use common\components\sms\SmsSenderInterface;

/**
 * 飞鸽短信驱动
 * @author cleverstone
 * @since ym2.0
 */
class Feige extends BaseObject implements SmsSenderInterface
{
    /**
     * @var string APP key
     */
    public $apikey;
    /**
     * @var string APP 秘钥
     */
    public $secret;
    /**
     * @var string APP 签名字符串
     */
    public $sign_id;
    /**
     * @var string API URL
     */
    public $apiUrl;

    /**
     * {@inheritdoc}
     */
    public function run($content, array $params)
    {
        $data = Http::req($this->apiUrl, 'post', 'json', [
            'apikey'  =>  $this->apikey,
            'secret' => $this->secret,
            'content' => $content,
            'mobile' => $params['mobile'],
            'sign_id' => $this->sign_id,
        ]);

        if ($data['code'] == 200) {
            $result = Json::decode($data['result']);
            if ($result['code'] == 0) {
                return true;
            }

            App::logError('飞鸽短信', $result);
            return $result['msg'];
        }

        App::logError('飞鸽短信', $data);
        return $data['result'];
    }
}